package com.kitten.comment.rpc;

import cn.hutool.core.collection.CollUtil;
import com.kitten.comment.model.bo.CommentBO;
import com.kitten.framework.common.constants.DateConstants;
import com.kitten.framework.common.response.Response;
import com.kitten.kv.api.KeyValueFeignApi;
import com.kitten.kv.dto.req.*;
import com.kitten.kv.dto.resp.FindCommentContentRspDTO;
import jakarta.annotation.Resource;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author kitten
 */
@Component
public class KeyValueRpcService {

    @Resource
    private KeyValueFeignApi keyValueFeignApi;

    public boolean batchSaveCommetContent(List<CommentBO> commentBOList) {
        List<CommentContentReqDTO> comments = Lists.newArrayList();
        for (CommentBO commentBO : commentBOList) {
            CommentContentReqDTO commentContentReqDTO = CommentContentReqDTO.builder()
                    .noteId(commentBO.getNoteId())
                    .yearMonth(commentBO.getCreateTime().format(DateConstants.DATE_FORMAT_Y_M))
                    .contentId(commentBO.getContentUuid())
                    .content(commentBO.getContent())
                    .build();
            comments.add(commentContentReqDTO);
        }
        // Feign 接口入参构造
        BatchAddCommentContentReqDTO reqDTO = BatchAddCommentContentReqDTO.builder()
                .comments(comments)
                .build();
        // Feign 接口调用
        Response<?> response = keyValueFeignApi.batchAddCommentContent(reqDTO);
        if (!response.isSuccess()) {
            throw new RuntimeException("error 评论内容保存失败");
        }
        return true;
    }

    /**
     * 批量查询评论内容
     * @param noteId
     * @param findCommentContentReqDTOS
     * @return
     */
    public List<FindCommentContentRspDTO> batchFindCommentContent(Long noteId, List<FindCommentContentReqDTO> findCommentContentReqDTOS) {
        BatchFindCommentContentReqDTO reqDTO = BatchFindCommentContentReqDTO.builder()
                .noteId(noteId)
                .commentContentKeys(findCommentContentReqDTOS)
                .build();
        // Feign 接口调用
        Response<List<FindCommentContentRspDTO>> response = keyValueFeignApi.batchFindCommentContent(reqDTO);
        if (!response.isSuccess() || Objects.isNull(response.getData()) || CollUtil.isEmpty(response.getData())) {
            return null;
        }
        return response.getData();
    }

    /**
     * 删除评论内容
     * @param noteId
     * @param createTime
     * @param contentId
     * @return
     */
    public boolean deleteCommentContent(Long noteId, LocalDateTime createTime, String contentId) {
        DeleteCommentContentReqDTO deleteCommentContentReqDTO = DeleteCommentContentReqDTO.builder()
                .noteId(noteId)
                .yearMonth(DateConstants.DATE_FORMAT_Y_M.format(createTime))
                .contentId(contentId)
                .build();

        // 调用 KV 存储服务
        Response<?> response = keyValueFeignApi.deleteCommentContent(deleteCommentContentReqDTO);

        if (!response.isSuccess()) {
            throw new RuntimeException("删除评论内容失败");
        }

        return true;
    }

}
